/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.generics.client.place;

import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.proxy.PlaceManagerImpl;
import com.gwtplatform.mvp.shared.proxy.PlaceRequest;
import com.gwtplatform.mvp.shared.proxy.TokenFormatter;
import javax.inject.Singleton;

/**
 *
 * @author v
 */
public class Place extends PlaceManagerImpl {

    private final PlaceRequest defaultPlaceRequest;
    private final PlaceRequest loginPlaceRequest;

    @Inject
    @Singleton
    public Place(EventBus eventBus, TokenFormatter tokenFormatter, @DefaultPlace final String defaultPlace,
            @LoginPlace final String loginPlace) {
        super(eventBus, tokenFormatter);

        this.defaultPlaceRequest = new PlaceRequest.Builder().nameToken(defaultPlace).build();
        this.loginPlaceRequest = new PlaceRequest.Builder().nameToken(loginPlace).build();
    }

    @Override
    public void revealDefaultPlace() {
        revealPlace(loginPlaceRequest, false);
    }

    @Override
    public void revealErrorPlace(String invalidHistoryToken) {
        revealPlace(new PlaceRequest.Builder(loginPlaceRequest).with("e", invalidHistoryToken).build(), true);
    }

    @Override
    public void revealUnauthorizedPlace(String unauthorizedHistoryToken) {
        PlaceRequest.Builder realPlaceBuilder = new PlaceRequest.Builder(loginPlaceRequest).with("q", getCurrentPlaceRequest().getNameToken());
        for (String param : getCurrentPlaceRequest().getParameterNames()) {
            realPlaceBuilder.with(param, getCurrentPlaceRequest().getParameter(param, null));
        }
        revealPlace(realPlaceBuilder.build(), true);
    }
}
